package com.wavecoler.professor.dao;

import javax.jdo.PersistenceManager;

import javax.jdo.Transaction;

import com.wavecoler.dao.PMF;
import com.wavecoler.professor.objects.Team;
import com.wavecoler.professor.objects.User;

import java.util.*;
import java.util.logging.Logger;

import javax.jdo.Query;

public class TeamDAO {

	private static final Logger log = Logger
			.getLogger(TeamDAO.class.getName());

	// Method to insert a new Team
	public static void insertTeam(Team Team) {
		PersistenceManager pm = null;

		// LLamamos a la clase que tendra la persistencia
		pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		try {
			tx.begin();
			pm.makePersistent(Team);
			tx.commit();
		} catch (Exception e) {
			log.warning(e.getMessage());
			log.info(e.getMessage());
			e.printStackTrace();
		} finally {
			if (tx.isActive()) {
				tx.rollback();
				log.warning("did transaction rollback");
			}
			if (pm != null)
				pm.close();
		}
	}

	// Method to get a list of Teams
	public static List<Team> getTeamsByName(String TeamName) {
		PersistenceManager pm = null;
		try {
			// LLamamos a la clase que tendra la persistencia
			pm = PMF.get().getPersistenceManager();
			Query query = pm.newQuery("select from " + Team.class.getName()
					+ " where TeamName == :p1");
			List<Team> Teams = (List<Team>) query.execute(TeamName);
			Teams.size();
			return Teams;
		} catch (Exception e) {
			log.warning(e.getMessage());
			log.info(e.getMessage());
			throw new RuntimeException(e);
		} finally {
			if (pm != null)
				pm.close();
		}
	}

	@SuppressWarnings("unchecked")
	// Method to return the list of Teams
	public static List<Team> getTeamsList() {
		PersistenceManager pm = null;
		try {
			pm = PMF.get().getPersistenceManager();
			Query query = pm.newQuery("select from " + Team.class.getName()
					+ "");
			List<Team> Teams = (List<Team>) query.execute();
			Teams.size();
			return Teams;
		} catch (Exception e) {
			log.info(e.getMessage());
			log.warning(e.getMessage());
			throw new RuntimeException(e);
		} finally {
			if (pm != null)
				pm.close();
		}
	}
	
	public static List<String> getTeamNames() {
		PersistenceManager pm = null;
		try {
			pm = PMF.get().getPersistenceManager();
			Query query = pm.newQuery("select TeamName from " + Team.class.getName()
					+ "");
			List<String> Teams = (List<String>) query.execute();
			Teams.size(); // es necesario agregarlo debido a bug -
							// http://www.atentia.net/2010/03/app-engine-and-jdo-object-manager-has-been-closed/
			log.info("Finished retrieving Team names.");
			log.warning("Finished retrieving Team names.");
			return Teams;
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			if (pm != null)
				pm.close();
		}
    }
	
	public static List<User> getTeamUsers(String TeamName) {
		PersistenceManager pm = null;
		try {
			pm = PMF.get().getPersistenceManager();
			
			Query query = pm.newQuery("select from " + Team.class.getName()
					+ " where TeamName == :p1");
			List<Team> Teams = (List<Team>) query.execute(TeamName);
			Teams.size();
			Team g = Teams.get(0);
			List<User> participants = g.getUsers();
			participants.size(); // es necesario agregarlo debido a bug -
							// http://www.atentia.net/2010/03/app-engine-and-jdo-object-manager-has-been-closed/
			String logMessage = "Finished retrieving Team users.  Users= ";
			for(int i = 0; i < participants.size(); i++){
				logMessage += "\n" + participants.get(i);
			}
			
			log.info(logMessage);
			log.warning("Finished retrieving Team users.");
			return participants;
		} catch (Exception e) {
			log.info("Problem loading users from Team '" + TeamName + "'.");
			throw new RuntimeException(e);
		} finally {
			if (pm != null)
				pm.close();
		}
    }
}